using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Collections;
using System.Collections.Specialized;

using SBPweb.Themes.Windows.Skins;
using SBPweb.Themes.Windows;

namespace SBPweb.Controls.Windows.Tab
{
	/// <summary>
	/// Class of TabControl
	/// </summary>
	[ToolboxItem(false)]
	public partial class TabControl : System.Windows.Forms.TabControl, ISkinnable, System.ComponentModel.ISupportInitialize
	{
		#region Private members

		private bool mUseDefaultSkin;
		private Skin mSkin;
		private Color mBackColor = Color.Transparent;

		#endregion

		#region Constructor logic

		/// <summary>
		/// Constructor of TabControl
		/// </summary>
		public TabControl()
		{
			SetStyle();
			InitializeComponent();
			
			RegisterEventHandlers();
		}

		/// <summary>
		/// Constructor of TabControl
		/// </summary>
		/// <param name="skin">skin</param>
		public TabControl(Skin skin)
		{
			mSkin = skin;
			SetStyle();
			InitializeComponent();

			RegisterEventHandlers();
		}

		private void SetStyle()
		{
			if (Skin != null)
			{
				base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
			}
			else
			{
				base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
				base.SetStyle(ControlStyles.UserPaint, false);
			}
		}

		private void RegisterEventHandlers()
		{
			base.ParentChanged += new EventHandler(this_OnParentChanged);
			DefaultLookAndFeel.OnSkinChanged += new EventHandler(DefaultLookAndFeel_OnSkinChanged);
		}

		#endregion

		#region Overrides
	
		/// <summary>
		/// Back color
		/// </summary>
		public override Color BackColor
		{
			get
			{
				return mBackColor;
			}
			set
			{
				mBackColor = value;
			}
		}

		#endregion

		#region Custom draw

		/// <summary>
		/// On paint
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnPaint(PaintEventArgs e)
		{
			if (Skin == null)
			{
				base.OnPaint(e);
			}
		}

		/// <summary>
		/// On background paint
		/// </summary>
		/// <param name="e">event args</param>
		protected override void OnPaintBackground(PaintEventArgs e)
		{
			base.OnPaintBackground(e);
		}

		#endregion

		#region Events

		private void DefaultLookAndFeel_OnSkinChanged(object sender, EventArgs e)
		{
			DoInvalidate();
		}

		private void DoInvalidate()
		{
			SetStyle();
			if(Skin != null)
			{
				this.BackColor = Skin.CachedProperties.ControlColor;
			}
			Invalidate(true);
		}

		private void this_OnParentChanged(object sender, EventArgs e)
		{
		}

		#endregion


		#region ISkinnable Members

		/// <summary>
		/// Use default skin?
		/// </summary>
		[Category("Look && feel")]
		public bool UseDefaultSkin
		{
			get
			{
				return mUseDefaultSkin;
			}
			set
			{
				mUseDefaultSkin = value;
				Invalidate();
			}
		}

		/// <summary>
		/// Used skin
		/// </summary>
		[Category("Look && feel")]
		public Skin Skin
		{
			get
			{
				if (this.UseDefaultSkin)
				{
					return DefaultLookAndFeel.Skin;
				}

				return mSkin;
			}
			set
			{
				mSkin = value;
				Invalidate();
			}
		}

		#endregion

		#region ISupportInitialize Members

		/// <summary>
		/// Start of init
		/// </summary>
		public void BeginInit()
		{
		}

		/// <summary>
		/// End of init
		/// </summary>
		public void EndInit()
		{
		}

		#endregion

	}
}
